import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { title: '首页', showTabbar: true }
  },
  {
    path: '/upload',
    name: 'Upload',
    component: () => import('../views/Upload.vue'),
    meta: { title: '上传照片', showTabbar: true }
  },
  {
    path: '/style',
    name: 'Style',
    component: () => import('../views/StyleSelect.vue'),
    meta: { title: '选择风格', showTabbar: false }
  },
  {
    path: '/generate',
    name: 'Generate',
    component: () => import('../views/Generate.vue'),
    meta: { title: '生成中', showTabbar: false }
  },
  {
    path: '/result/:taskId',
    name: 'Result',
    component: () => import('../views/Result.vue'),
    meta: { title: '生成结果', showTabbar: false },
    props: true
  },
  {
    path: '/history',
    name: 'History',
    component: () => import('../views/History.vue'),
    meta: { title: '历史记录', showTabbar: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue'),
    meta: { title: '我的', showTabbar: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录', showTabbar: false }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 喵趣AI`
  }
  
  // 检查登录状态（简化处理）
  const token = localStorage.getItem('token')
  const needAuth = ['Upload', 'Generate', 'Result', 'History', 'Profile']
  
  if (needAuth.includes(to.name as string) && !token) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
  } else {
    next()
  }
})

export default router